/**
 * @description: iframe hook
 * @param {String} url iframe src
 */
import { ref } from "vue";

const useIframe = (url, target) => {
  const iframe = ref(null);
  // 创建iframe
  const createIframe = () => {
    iframe.value = document.createElement("iframe");
    iframe.value.src = url;
    iframe.value.style.display = "none";
    document.body.appendChild(target);
  };
  // 销毁iframe
  const destroyIframe = () => {
    if (iframe.value) {
      try {
        iframe.value.src = "about:blank";
      } catch (error) {
        iframe.value.src = "javascript:void(0)";
      }

      try {
        iframe.value.document.write("");
        iframe.value.document.clear();
        // 以上操作已删除了大量资源
      } catch (e) {}
    }
    document.body.removeChild(iframe.value);
    iframe.value = null;
  };

  return {
    iframe,
    destroyIframe,
    createIframe
  };
};
